package com.amkai.mobile.client.options;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;

import com.amkai.mobile.bean.organization.MobileBusinessGroup;
import com.amkai.mobile.client.R;
import com.amkai.mobile.client.common.AEMobileConstants;
import com.amkai.mobile.client.common.SharedDataProvider;
import com.amkai.mobile.common.job.organization.MobileOrganizationJobResult;

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

	private CharSequence[] businessGroups = null;
	private CharSequence[] businessGroupValues = null;
	private CharSequence[] businessEntities = null;
	private CharSequence[] businessEntityValues = null;
	ListPreference preferences_bg_list;
	ListPreference preferences_be_list;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		setTitle(AEMobileConstants.APP_TITLE  +" - " + "Preferences");
		preferences_bg_list = (ListPreference) findPreference("preferences_bg");
		preferences_be_list = (ListPreference) findPreference("preferences_be");
//		SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
//		String bg = sp.getString("preferences_bg", "");
		

		loadall();
		
		preferences_bg_list.setSummary(preferences_bg_list.getEntry());
		preferences_be_list.setSummary(preferences_be_list.getEntry());

		CheckBoxPreference autostart = (CheckBoxPreference) findPreference("autologin");
		if (autostart.isChecked())
			autostart.setSummary("On");
		else
			autostart.setSummary("Off");
		
		preferences_bg_list.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

			@Override
			public boolean onPreferenceChange(Preference arg0, Object arg1) {
				// TODO Auto-generated method stub
				MobileBusinessGroup mbg = null;
				for (MobileBusinessGroup mbg1 : getMojr(Preferences.this).businessGroups) {
					if (mbg1.id.toString().equals(arg1)) {
						mbg = mbg1;
					}
				}

				loadBeList(mbg);
				return true;
			}
		});
	}

	private void loadall() {
		businessGroups = new String[getMojr(this).businessGroups.size()];
		businessGroupValues = new CharSequence[getMojr(this).businessGroups.size()];

		for (int i = 0; i < getMojr(this).businessGroups.size(); i++) {
			businessGroups[i] = getMojr(this).businessGroups.get(i).name;
			businessGroupValues[i] = getMojr(this).businessGroups.get(i).id.toString();
		}
		preferences_bg_list.setEntries(businessGroups);
		preferences_bg_list.setEntryValues(businessGroupValues);
		loadBeList(getMojr(this).businessGroups.get(0));

	}

	protected void loadBeList(MobileBusinessGroup mbg) {
		businessEntities = new String[mbg.businessEntities.size()];
		businessEntityValues = new String[mbg.businessEntities.size()];
		for (int i = 0; i < mbg.businessEntities.size(); i++) {
			businessEntities[i] = mbg.businessEntities.get(i).name;
			businessEntityValues[i] = mbg.businessEntities.get(i).id.toString();
		}
		preferences_be_list.setEntries(businessEntities);
		preferences_be_list.setEntryValues(businessEntityValues);

	}
	
	  @Override
	  protected void onPause() {
	    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	    finish();
	    super.onPause();
	  }
	  
	  protected void onResume() {
		    super.onResume();
		    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	  }

	@Override
	public void onSharedPreferenceChanged(SharedPreferences arg0, String key) {
		Preference pref = findPreference(key);
		if (pref instanceof ListPreference) {
			ListPreference etp = (ListPreference) pref;
			pref.setSummary(etp.getEntry());

		} else if (pref instanceof CheckBoxPreference) {
			if (((CheckBoxPreference) pref).isChecked())
				pref.setSummary("On");
			else
				pref.setSummary("Off");
		}

	}

	public MobileOrganizationJobResult getMojr(Activity context) {
		MobileOrganizationJobResult result = null;
		try {
			result =  SharedDataProvider.getInstance().getMojr(context);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

}